package com.woniu.permissions.controller;


import cn.hutool.json.JSONUtil;
import com.woniu.model.Result;
import com.woniu.permissions.dto.MenuDto;
import com.woniu.permissions.model.TMenu;
import com.woniu.permissions.service.TMenuService;
import com.woniu.util.RedisUtil;
import com.woniu.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年06月14日
 */
@RestController
@RequestMapping("/tMenu")
@Api(tags = "动态菜单服务")
public class TMenuController {


    @Resource
    private RedisUtil redisUtil;

    @Resource
    private TMenuService tMenuService;


    @GetMapping("/myMenu")
    @ApiOperation("查询个人所有权限菜单,若num为空则为用户登录，非空为后端人员登录")
    public Result<List<MenuDto>> menu(@RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        Long userId =null;
        try {
            userId = Long.valueOf(JwtUtil.parseToken(refreshToken,JwtUtil.employSecret).get("accountId").toString());
        } catch (Exception e) {
            try {
//                userId = Long.valueOf(JwtUtil.parseToken(refreshToken,JwtUtil.stationBossSecret).get("accountId").toString());
                System.out.println(userId);
                Claims claims = JwtUtil.parseToken(refreshToken, JwtUtil.stationBossSecret);
                System.out.println(JSONUtil.toJsonStr(claims));
                Object accountId1 = claims.get("accountId",Long.class);
                System.out.println(accountId1);
                String accountId = accountId1.toString();
                userId = Long.valueOf(accountId);
            } catch (Exception ex) {
//                userId = Long.valueOf(JwtUtil.parseToken(refreshToken,JwtUtil.adminSecret).get("accountId").toString());
        System.out.println(userId);
        Claims claims = JwtUtil.parseToken(refreshToken, JwtUtil.adminSecret);
        System.out.println(JSONUtil.toJsonStr(claims));
        Object accountId1 = claims.get("accountId",Long.class);
        System.out.println(accountId1);
        String accountId = accountId1.toString();
        userId = Long.valueOf(accountId);
            }
        }


        List<MenuDto> menu = tMenuService.selectMenu(userId);
        System.out.println(menu);
        System.out.println("=====================menu====================");
        return Result.newListInstance(MenuDto.class).setData(menu);
    }

}

